package com.common.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.domain.dto.ExchangeLogDto;
import com.common.domain.entity.ExchangeLog;
import com.common.domain.entity.Goods;
import com.common.domain.entity.Point;
import com.common.domain.entity.ResponseResult;
import com.common.domain.vo.ExchangeLogVo;
import com.common.domain.vo.PageVo;
import com.common.domain.vo.PointVo;
import com.common.mapper.ExchangeLogMapper;
import com.common.mapper.GoodsMapper;
import com.common.mapper.PointMapper;
import com.common.service.ExchangeLogService;
import com.common.service.GoodsService;
import com.common.util.BeanCopyUtils;
import com.common.util.SecurityUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * (ExchangeLog)表服务实现类
 *
 * @author makejava
 * @since 2023-09-29 13:11:48
 */
@Service
public class ExchangeLogServiceImpl extends ServiceImpl<ExchangeLogMapper, ExchangeLog> implements ExchangeLogService {

    @Autowired
    private ExchangeLogMapper exchangeLogMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private PointMapper pointMapper;

    @Override
    public ResponseResult getUserExchangeLog(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        ExchangeLogDto exchangeLogDto = new ExchangeLogDto();
        exchangeLogDto.setUserId(SecurityUtils.getUserId());

        List<ExchangeLogVo> list =  exchangeLogMapper.getUserExchangeLog(exchangeLogDto);

        PageInfo<ExchangeLogVo> pageInfo = new PageInfo<>(list);

        pageInfo.setList(list);

        return ResponseResult.okResult(pageInfo);
    }

    @Override
    public ResponseResult getGoodsInfo() {
        List<Goods> goods = goodsMapper.getGoodsInfo(null);
        //获取当前用户id
        Long userId = SecurityUtils.getUserId();
        //得到用户剩余积分
        Long userPoint = pointMapper.getUserPointById(userId);

        JSONObject returnData = new JSONObject();
        returnData.put("point", userPoint);
        returnData.put("goodsInfo",goods);
        return ResponseResult.okResult(returnData);
    }

    @Override
    public ResponseResult allExchangeList(ExchangeLogDto exchangeLogDto) {
        PageHelper.startPage(exchangeLogDto.getPageNum(),exchangeLogDto.getPageSize());

        List<ExchangeLogVo> list =  exchangeLogMapper.getUserExchangeLog(exchangeLogDto);

        PageInfo<ExchangeLogVo> pageInfo = new PageInfo<>(list);

        pageInfo.setList(list);

        return ResponseResult.okResult(pageInfo);
    }

    @Override
    public ResponseResult deleteExchange(String ids) {
        String[] split = ids.split(",");
        removeByIds(Arrays.asList(split));
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult changeStatus(Long exchangeId, String status) {
        //先找到
        LambdaQueryWrapper<ExchangeLog> wrapper = new LambdaQueryWrapper();
        wrapper.eq(ExchangeLog::getId,exchangeId);
        List<ExchangeLog> list = list(wrapper);
        //改变值
        list.get(0).setReceiveFlag(status);
        //修改
        update(list.get(0),wrapper);
        return ResponseResult.okResult();
    }
}

